<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp"%>
<html>
<head>
<meta charset="UTF-8">
<title>用户中心-账户提现</title>
<jsp:include page="/include/wchat/head.jsp"></jsp:include>

<link type="text/css" rel="stylesheet" href="/v2/wchat/resrc/css/account.css">
</head>
<body>
<div id="header"> <a href="javascript:history.go(-1);" class="backto">返回</a>
  <div class="title-sub">账户提现</div>
</div>
<div id="wrap">
  <div class="account-balance">
    <div class="link-touch">
      <ul class="items">
        <li><span class="txt">持卡人</span> <span type="password" class="value">${session.user.realName}</span></li>
        <li><span class="txt">提现至银行卡</span> 
        	<s:if test="#request.banks.size>0">
        		<input type="hidden" name="hasBankList" id="hasBankList" value="1">
        	</s:if>
        	<s:else>
        		<input type="hidden" name="hasBankList" id="hasBankList" value="-1">
        	</s:else>
        	<span type="password" class="value" style="float:left;width:60%;">
	        	<select class="select" id="bank" name="bank" onchange="setBankId();" style="margin-top:5px;">
	        	<option value="-1">请选择</option>	
	        	<s:iterator value="#request.banks" status="stuts" id="bank">
		            <option value="${bank.id}">${bank.bankName} **${bank.formatCardNo }</option>
				</s:iterator>
	           </select>
        	</span>
        	
        </li>
        <li><span class="txt">可用余额</span> <span type="password" class="value">￥<fmt:formatNumber value="${usableSum}" pattern="#,##0.00#"/></span></li>
        <li><span class="txt">可免费提现次数</span> <span id="freeWithdrawal" type="password" class="value" style="color:#ff3000;">次</span> </li>
        <li><span class="txt">提现金额</span>
        	<form id="withdrawForm" style="-webkit-box-flex:1;-webkit-flex:1;height:20px; line-height:20px;border:0; padding:12px 0 13px;flex:1;" method="post" action="wchatWithdraw_step2.do">
				<input type="hidden" name="bankId" id="bankId" value="">
				<input type="text" class="input" style="width:100%;padding:0;" name="withdrawMoney" id="withdrawMoney" maxlength="12" placeholder="输入提现金额">
			</form>
			<span style="display: none;padding:0 5px;" id="withdrawCashErr">余额不足</span>
      	</li>
      	<li><span class="txt">手续费</span> <span id="withdrawFee" type="password" class="value">￥0.00<fmt:formatNumber value="${accmountStatisMap.usableAmount}" pattern="#,##0.00#"/></span></li>
      	<li><span class="txt">实际到账金额</span> <span id="realTackOut" type="password" class="value">￥0.00<span class="err none" id="errMsg">可用余额不足</span></span></li>
      </ul>
    </div>
    <div class="operate-button"> <a class="btn b_next">下一步</a> </div>
    <div class="itip">警告：禁止信用卡套现、虚假交易等行为，一经确认，将终止该账户的使用。</div>
    <div class="itip">您的资金将从合作银行监管账户转入提现银行卡</div>
    <input type="hidden" id="withdrawFeeRate" value="${FeeRate/100}">
	<input type="hidden" id="minWithdrawFee" value="5.00">
	<input type="hidden" id="withdrawNum" value="${withdrawNum}">
  </div>
</div>

<jsp:include page="/include/wchat/footer.jsp"></jsp:include>
<script type="text/javascript">
$(function(){

    calcWithDrawNum();
    
    if($("#withdrawMoney").val() != ''){
        calcWithDrawFee();
    }
    
    $("#withdrawMoney").keyup(function(){
        calcWithDrawFee();
    });
    
}); 
$(function(){
	$(".b_next").click(function(e){
		
		var bankId = $("#bankId").val();
		var hasBankList = $("#hasBankList").val();
		if(hasBankList < 0){
			alert("请先绑定一张用于提现的银行卡");
	        return;
	    }
		if(bankId == undefined || bankId == '' || bankId < 0){
			alert("请先单击选择一张已绑定的提现银行卡");
	        return;
		}
	    if($("#withdrawMoney").val() > 0 && Number($("#realTackOut").html()) <= 0){
	    	$("#errMsg").html("到账金额不足").show();
	    	return;
	    }
		if(checkWithdrawCash()){
			var form = $("#withdrawForm");
			form.submit();
		}
	});

  
    function checkWithdrawCash() {
		var balance = Number(environment.userCash);
		var withdrawCash = Number($("#withdrawMoney").val());
		if(withdrawCash == "" || withdrawCash == null){
			$("#withdrawCashErr").html('<i class="icons reg-error"></i>输入取款金额').show();
			return false;			
		}else if (withdrawCash != "" && withdrawCash != null) {
			var reg =/^[0-9]+([.]\d{1,2})?$/;
			if (!reg.test(withdrawCash)) {
				$("#withdrawCashErr").html('<i class="icons reg-error"></i>输入金额有误').show();
				return false;
			}
            else if(withdrawCash < 100){
                $("#withdrawCashErr").html('<i class="icons reg-error"></i>最小提现金额100元起').show();
                return;
            }else {
				if ((withdrawCash-balance)>0) {
					$("#withdrawCashErr").html('<i class="icons reg-error"></i>余额不足').show();
					return false;
				}else if(withdrawCash==0){
					$("#withdrawCashErr").html('<i class="icons reg-error"></i>输入金额有误').show();
					return false;
				}else{
					$("#withdrawCashErr").html("").hide();;
					return true;					
				}
			}
		}
	}
    

    
});

var freeWithdrawNum = Number('${freeNum}');//免费提现次数
var withdrawNum = Number('${withdrawNum}');//当月已提现次数
var withdrawFeeRate = Number($("#withdrawFeeRate").val());
var minWithdrawFee = Number($("#minWithdrawFee").val());

function calcWithDrawNum(){
	var canWithDrawNum = freeWithdrawNum - withdrawNum < 0 ? 0:freeWithdrawNum - withdrawNum;
	$("#freeWithdrawal").html(canWithDrawNum + '次');
}

function calcWithDrawFee(){
    $("#errMsg").hide();
    if(isNaN($("#withdrawMoney").val())){
        $("#withdrawCashErr").html('<i class="icons reg-error"></i>请输入正确的金额').show();
        return;
    }
    var amount = Number($("#withdrawMoney").val());
    var cash = Number('${usableSum}');
    if(amount > cash){
        $("#withdrawMoney").val(cash);
        amount = cash;
    }

    var withdrawFee = 0;
    if(withdrawNum >= freeWithdrawNum){
        withdrawFee = amount * withdrawFeeRate;
        if(withdrawFee < minWithdrawFee){
            withdrawFee = minWithdrawFee;
        }
    }
    withdrawFee = Math.ceil(withdrawFee*100)/100;
    $("#withdrawFee").html(Util.numFormat(withdrawFee,2));
    var realTackOut = (amount-withdrawFee) < 0 ? 0 : (amount-withdrawFee);
    $("#realTackOut").html(Util.numFormat(realTackOut,2));
    if(amount > 0 && amount-withdrawFee <= 0){
        $("#errMsg").html("到账金额不足").show();
    }
}
function setBankId() {
	$("#bankId").val($("#bank").val());
}
</script>

</body>
</html>